package com.binance.notification.web.controller;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.binance.master.error.BusinessException;
import com.binance.master.models.APIRequest;
import com.binance.master.models.APIRequest.VoidBody;
import com.binance.master.models.APIResponse;
import com.binance.notification.api.DeviceApi;
import com.binance.notification.api.request.AliasTagsRequest;
import com.binance.notification.api.request.CountUidsByTagAndPlatformRequest;
import com.binance.notification.api.request.GetAliasDeviceListRequest;
import com.binance.notification.api.request.GetUidListByAliasAndPlatformRequest;
import com.binance.notification.api.request.GetWebSDKInitParamsRequest;
import com.binance.notification.api.request.SetAliasAndTagsRequest;
import com.binance.notification.api.request.SetLanguageRequest;
import com.binance.notification.api.response.AliasTagsResponse;
import com.binance.notification.api.response.WebSDKInitParamsResponse;
import com.binance.notification.web.service.IDeviceService;

import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class DeviceController implements DeviceApi {

    @Autowired
    private IDeviceService deviceService;

    @Override
    public APIResponse<Set<String>> languageTags(@RequestBody @Validated APIRequest<VoidBody> request) {
        log.info("languageTags......");
        try {
            return APIResponse.getOKJsonResult(this.deviceService.languageTags());
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                log.warn("查询所有语言标签出现异常", e);
                throw (BusinessException) e;
            }
            log.error("查询所有语言标签出现异常", e);
            throw new RuntimeException(e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public APIResponse<AliasTagsResponse> aliasTags(@RequestBody @Validated APIRequest<AliasTagsRequest> request) {
        log.info("aliasTags，参数registrationId={}", request.getBody().getRegistrationId());
        try {
            Map<String, Object> map = this.deviceService.aliasTags(request.getBody().getRegistrationId());
            AliasTagsResponse resp = new AliasTagsResponse();
            resp.setAlias((String) map.get("alias"));
            resp.setTags((List<String>) map.get("tags"));
            return APIResponse.getOKJsonResult(resp);
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                log.warn("查询设备" + request.getBody().getRegistrationId() + "所有标签出现异常", e);
                throw (BusinessException) e;
            }
            log.error("查询设备" + request.getBody().getRegistrationId() + "所有标签出现异常", e);
            throw new RuntimeException(e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public APIResponse<AliasTagsResponse> setAliasAndTags(
            @RequestBody @Validated APIRequest<SetAliasAndTagsRequest> request) {
        log.info("setAliasAndTags，参数registrationId={},alias={},tagsToAdd={},tagsToRemove={}",
                request.getBody().getRegistrationId(), request.getBody().getAlias(), request.getBody().getTagsToAdd(),
                request.getBody().getTagsToRemove());
        try {
            Map<String, Object> map = this.deviceService.setAliasAndTags(request.getBody().getRegistrationId(),
                    request.getBody().getAlias(), request.getBody().getTagsToAdd(),
                    request.getBody().getTagsToRemove());
            AliasTagsResponse resp = new AliasTagsResponse();
            resp.setAlias((String) map.get("alias"));
            resp.setTags((List<String>) map.get("tags"));
            return APIResponse.getOKJsonResult(resp);
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                log.warn("setAliasAndTags出现异常", e);
                throw (BusinessException) e;
            }
            log.error("setAliasAndTags出现异常", e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public APIResponse<WebSDKInitParamsResponse> getWebSDKInitParams(
            @RequestBody @Validated APIRequest<GetWebSDKInitParamsRequest> request) {
        log.info("getWebSignature，参数strValue={}", request.getBody().getUserStr());
        try {
            return APIResponse.getOKJsonResult(this.deviceService.getWebSDKInitParams(request.getBody().getUserStr()));
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                log.warn("getWebSignature出现异常", e);
                throw (BusinessException) e;
            }
            log.error("getWebSignature出现异常", e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public APIResponse<Void> setLanguage(@RequestBody @Validated APIRequest<SetLanguageRequest> request) {
        log.info("setLanguage，参数registrationId={},language={}", request.getBody().getRegistrationId(),
                request.getBody().getLanguage());
        try {
            this.deviceService.setLanguage(request.getBody().getRegistrationId(), request.getBody().getLanguage());
            return APIResponse.getOKJsonResult(null);
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                log.warn("setLanguage出现异常", e);
                throw (BusinessException) e;
            }
            log.error("setLanguage出现异常", e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public APIResponse<List<String>> getAliasDeviceList(
            @RequestBody @Validated APIRequest<GetAliasDeviceListRequest> request) {
        log.info("getAliasDeviceList，参数alias={},platform={}", request.getBody().getAlias(),
                request.getBody().getPlatform());
        try {
            return APIResponse.getOKJsonResult(this.deviceService.getAliasDeviceList(request.getBody().getAlias(),
                    request.getBody().getPlatform()));
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                log.warn("getAliasDeviceList出现异常", e);
                throw (BusinessException) e;
            }
            throw new RuntimeException(e);
        }
    }

    @Override
    public APIResponse<Long> countUidsByTagAndPlatform(
            @RequestBody @Validated APIRequest<CountUidsByTagAndPlatformRequest> request) {
        log.info("countUidsByTagAndPlatform，参数tag={},platform={}", request.getBody().getTag(),
                request.getBody().getPlatform());
        try {
            Long count = this.deviceService.countUidsByTagAndPlatform(request.getBody().getTag(),
                    request.getBody().getPlatform());
            return APIResponse.getOKJsonResult(count);
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                log.warn("countUidsByTagAndPlatform", e);
                throw (BusinessException) e;
            }
            throw new RuntimeException(e);
        }
    }

    @Override
    public APIResponse<List<String>> getUidListByAliasAndPlatform(
            @RequestBody @Validated APIRequest<GetUidListByAliasAndPlatformRequest> request) {
        log.info("getUidListByAliasAndPlatform，参数alias={},platform={}", request.getBody().getAlias(),
                request.getBody().getPlatform());
        try {
            return APIResponse.getOKJsonResult(this.deviceService
                    .getUidListByAliasAndPlatform(request.getBody().getAlias(), request.getBody().getPlatform()));
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                log.warn("getUidListByAliasAndPlatform", e);
                throw (BusinessException) e;
            }
            throw new RuntimeException(e);
        }
    }

}
